package com.yanqu.road.server.manager.activity.cat;

import com.yanqu.road.dao.impl.activity.cat.CatDrawNoticeDaoImpl;
import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.cat.data.CatDrawNotice;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.CatProto;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CatActivity {

    private ActivityInfo activityInfo;

    private List<CatDrawNotice> noticeList;

    Map<String, ActivityConfig> activityConfigMap;

    int count = 50;

    public CatActivity(ActivityInfo activityInfo) {
        this.activityInfo = activityInfo;
    }

    public void init(){
        List<Integer> activityIdList = new ArrayList<>();
        activityIdList.add(activityInfo.getActivityId());

        activityConfigMap = ActivityBussiness.getActivityConfigMap(activityIdList).get(activityInfo.getActivityId());

        if(activityConfigMap.containsKey(eActivityConfigType.CAT_LOTTERY_LOG_LIMIT.getName())){
            count = activityConfigMap.get(eActivityConfigType.CAT_LOTTERY_LOG_LIMIT.getName()).getIntValue();
        }

        noticeList = new CatDrawNoticeDaoImpl().getCatDrawNotice(activityInfo.getActivityId(), count);
    }

    public void setActivityInfo(ActivityInfo activityInfo) {
        this.activityInfo = activityInfo;
    }

    public ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public List<CatDrawNotice> getNoticeList() {
        return noticeList;
    }

    public void save() {
        int size = noticeList.size();
        for (int i = 0; i < size; i++) {
            CatDrawNotice notice = noticeList.get(i);
            if (notice.isInsertOption()) {
                new CatDrawNoticeDaoImpl().add(notice);
            }
        }
    }


    /**
     * 添加公告
     */
    public void uploadNotice(long userId, int rewardId, UserBaseInfo userBaseInfo, int type, String reward) {
        CatDrawNotice notice = new CatDrawNotice();
        notice.setActivityId(activityInfo.getActivityId());
        notice.setUserId(userId);
        notice.setUserBaseInfo(userBaseInfo);
        notice.setRewardId(rewardId);
        notice.setTime(System.currentTimeMillis());
        notice.setType(type);
        notice.setReward(reward);
        notice.setInsertOption();

        synchronized (noticeList) {
            noticeList.add(notice);
        }

        // 同步到区服
        CatProto.SyncCatNoticeRespMsg.Builder builder = CatProto.SyncCatNoticeRespMsg.newBuilder();
        builder.setNotice(CrossCatActivityMgr.parseNoticeMsg(notice));
        YanQuMessage yanQuMessage = YanQuMessageUtils.buildMessage(GameProtocol.S_CAT_SYNC_NOTICE, builder);
        for (Long serverId : activityInfo.getServerIdList()) {
            MessageHelper.sendPacket(serverId, 0, yanQuMessage);
        }
    }


    /**
     * 获取公告
     */
    public List<CatDrawNotice> getNoticeData() {
        List<CatDrawNotice> tmpList = new ArrayList<>(noticeList);
        if (tmpList.size() > count) {
            List<CatDrawNotice> result = new ArrayList<>();
            int size = tmpList.size();
            for (int i = size - 1; i >= size - count ; i--) {
                result.add(tmpList.get(i));
            }
            return tmpList.subList(tmpList.size() - count, tmpList.size());
        } else {
            return tmpList;
        }
    }
}
